#include "mycoin.h"
#include <QPixmap>
#include <QDebug>
MyCoin::MyCoin(QString BtnImage){
    QPixmap pix;
    bool ret = pix.load(BtnImage);
    if(!ret){
        QString s = QString("图片%1加载失败").arg(BtnImage);
        qDebug()<<s;
        return;
    }
    pix = pix.scaled(pix.width()*2,pix.height()*2);
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(QPixmap(pix));
    this->setIconSize(QSize(pix.width(),pix.height()));
    timer1 = new QTimer(this);
    timer2 = new QTimer(this);

    connect(timer1,&QTimer::timeout,[=](){
       QPixmap pix;
       QString coinPath = QString(":/res/Coin000%1.png").arg(min++);
       pix.load(coinPath);
       pix = pix.scaled(pix.width()*2,pix.height()*2);
       this->setFixedSize(pix.width(),pix.height());
       this->setStyleSheet("QPushButton{border:0px;}");
       this->setIcon(QIcon(pix));
       this->setIconSize(QSize(pix.width(),pix.height()));
       if(min>max){
           min = 1;
           this->isAnimation = false;
           timer1->stop();
       }
    });

    connect(timer2,&QTimer::timeout,[=](){
        QPixmap pix;
        QString coinPath = QString(":/res/Coin000%1.png").arg(max--);
        pix.load(coinPath);
        pix = pix.scaled(pix.width()*2,pix.height()*2);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(QIcon(pix));
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(max<min){
            max = 8;
            this->isAnimation = false;
            timer2->stop();
        }
    });
}

void MyCoin::mousePressEvent(QMouseEvent * ev){
    if(this->isAnimation || this->isWin){
        return;
    }else{
        QPushButton::mousePressEvent(ev);
    }
}

void MyCoin::changeFlag(){
    if(this->flag){
        coinFlipSound->play();
        timer1->start(50);
        this->isAnimation = true;
        this->flag=false;
    }else if(!this->flag){
        coinFlipSound->play();
        timer2->start(50);
        this->isAnimation = true;
        this->flag=true;
    }
}
